package cn.agiledata.bank.linkbank.impl.validate;

import cn.agiledata.bank.common.util.StringUtil;
import cn.agiledata.bank.linkbank.exception.LinkBankException;

/**
 * 数据域格式校验器
 * 
 * @author maxianbo
 * 
 */
public final class FieldFormatValidator extends AbstractFieldValidator {
    private String pattern;

    public FieldFormatValidator() {
        setEmptiable(true); // 默认允许为空字符串，通过正则表达式具体判断空字符串是否被允许
    }

    public void setPattern(String pattern) {
        this.pattern = pattern;
    }

    public void validate(String value) throws LinkBankException {
        if (value == null) {
            validateNullable(value);
        } else if ("".equals(value)) {
            validateEmptiable(value);
        } else {
            if (!StringUtil.matchRegex(value, this.pattern)) {
                throw LinkBankException.getFormatUnmatchException(getTarget(),
                        value, getErrorCode());
            }
        }
    }

}
